package com.ruoyi.supplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.supplier.domain.bo.ProductSpecValueBo;
import com.ruoyi.supplier.domain.vo.ProductSpecValueVo;
import com.ruoyi.supplier.domain.ProductSpecValue;
import com.ruoyi.supplier.mapper.ProductSpecValueMapper;
import com.ruoyi.supplier.service.IProductSpecValueService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品规格值记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class ProductSpecValueServiceImpl implements IProductSpecValueService {

    private final ProductSpecValueMapper baseMapper;

    /**
     * 查询商品规格值记录
     */
    @Override
    public ProductSpecValueVo queryById(Integer specValueId){
        return baseMapper.selectVoById(specValueId);
    }

    /**
     * 查询商品规格值记录列表
     */
    @Override
    public TableDataInfo<ProductSpecValueVo> queryPageList(ProductSpecValueBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProductSpecValue> lqw = buildQueryWrapper(bo);
        Page<ProductSpecValueVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品规格值记录列表
     */
    @Override
    public List<ProductSpecValueVo> queryList(ProductSpecValueBo bo) {
        LambdaQueryWrapper<ProductSpecValue> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ProductSpecValue> buildQueryWrapper(ProductSpecValueBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ProductSpecValue> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSpecValue()), ProductSpecValue::getSpecValue, bo.getSpecValue());
        lqw.eq(bo.getSpecId() != null, ProductSpecValue::getSpecId, bo.getSpecId());
        lqw.eq(bo.getMerchantId() != null, ProductSpecValue::getMerchantId, bo.getMerchantId());
        return lqw;
    }

    /**
     * 新增商品规格值记录
     */
    @Override
    public Boolean insertByBo(ProductSpecValueBo bo) {
        ProductSpecValue add = BeanUtil.toBean(bo, ProductSpecValue.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSpecValueId(add.getSpecValueId());
        }
        return flag;
    }

    /**
     * 修改商品规格值记录
     */
    @Override
    public Boolean updateByBo(ProductSpecValueBo bo) {
        ProductSpecValue update = BeanUtil.toBean(bo, ProductSpecValue.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ProductSpecValue entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品规格值记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean deleteBySpecId(Integer specId) {
        return baseMapper.delete(Wrappers.lambdaQuery(ProductSpecValue.class).eq(ProductSpecValue::getSpecId,specId)) > 0;
    }
}
